package com.lzx.iteam;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.SalaryWXYJAdapter;
import com.lzx.iteam.base.FindViewById;
import com.lzx.iteam.base.MyBaseActivity;
import com.lzx.iteam.bean.SalaryRuleBean;
import com.lzx.iteam.bean.SalaryTaxBean;
import com.lzx.iteam.net.AsynHttpClient;
import com.lzx.iteam.net.LocalLogin;
import com.lzx.iteam.net.WithoutObjMsg;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.MyListView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2017年 01月 10日
 * Time: 上午10:48
 *
 * @QQ : 1234567890
 * **********************************
 *
 * 五险一金比例设置页面
 */
public class SalaryWXYJActivity extends MyBaseActivity implements View.OnClickListener {

    @FindViewById(id = R.id.title_ll_back)
    private LinearLayout mLayoutBack;
    @FindViewById(id = R.id.title_tv_center)
    private TextView mTvTitle;
    @FindViewById(id = R.id.title_tv_right)
    private TextView mTvRight;
    @FindViewById(id = R.id.lv_wxyj)
    private MyListView mListView;


    private SalaryWXYJAdapter mAdapter;
    private ArrayList<SalaryTaxBean> mDatas = new ArrayList<>();
    private HashMap<String,Float> mPerson = new HashMap<>();
    private HashMap<String,Float> mCompany = new HashMap<>();
    private SalaryRuleBean mRuleData;

    private String mGroupId;

    private final int SET_SALARY_WXYJ = 30001;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            waitDlgDismiss();
            if (msg.arg1 == 0){
                switch (msg.what){
                    case SET_SALARY_WXYJ:
                        showShortToast("修改成功");
                        mRuleData.setP_pension_pc(mPerson.get("养老"));
                        mRuleData.setP_medical_pc(mPerson.get("医疗"));
                        mRuleData.setP_medical_count(mPerson.get("医疗+"));
                        mRuleData.setP_provident_fund_pc(mPerson.get("公积金"));
                        mRuleData.setP_unemployment_pc(mPerson.get("失业"));

                        mRuleData.setC_pension_pc(mCompany.get("养老"));
                        mRuleData.setC_medical_pc(mCompany.get("医疗"));
                        mRuleData.setC_provident_fund_pc(mCompany.get("公积金"));
                        mRuleData.setC_unemployment_pc(mCompany.get("失业"));
                        mRuleData.setC_injury_pc(mCompany.get("工伤"));
                        mRuleData.setC_birth_pc(mCompany.get("生育"));
                        Intent intent = new Intent();
                        intent.putExtra("salary_rule",mRuleData);
                        setResult(RESULT_OK,intent);
                        finish();
                        break;

                }
            }else{
                Toast.makeText(mActivity, (String) msg.obj,
                      Toast.LENGTH_LONG).show();
            }

        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState,R.layout.salary_wxyj_layout);
    }

    @Override
    public void initListener() {
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
    }

    @Override
    public void initData() {

        mTvTitle.setText("五险一金比例");
        mTvRight.setText("保存");

        Intent intent = getIntent();
        mGroupId = intent.getStringExtra("group_id");
        mRuleData = (SalaryRuleBean) intent.getSerializableExtra("salary_rule");

        mDatas.add(new SalaryTaxBean(0,"个人缴纳"));
        mDatas.add(new SalaryTaxBean(1,"养老",mRuleData.getP_pension_pc() + ""));
        mDatas.add(new SalaryTaxBean(1,"失业",mRuleData.getP_unemployment_pc() + ""));
        mDatas.add(new SalaryTaxBean(1,"医疗",mRuleData.getP_medical_pc() + ""));
        mDatas.add(new SalaryTaxBean(3,"医疗+",mRuleData.getP_medical_count() + ""));
        mDatas.add(new SalaryTaxBean(1,"公积金",mRuleData.getP_provident_fund_pc() + ""));

        mDatas.add(new SalaryTaxBean(0,"公司缴纳"));
        mDatas.add(new SalaryTaxBean(2,"养老",mRuleData.getC_pension_pc() + ""));
        mDatas.add(new SalaryTaxBean(2,"失业",mRuleData.getC_unemployment_pc() + ""));
        mDatas.add(new SalaryTaxBean(2,"工伤",mRuleData.getC_injury_pc() + ""));
        mDatas.add(new SalaryTaxBean(2,"生育",mRuleData.getC_birth_pc() + ""));
        mDatas.add(new SalaryTaxBean(2,"医疗",mRuleData.getC_medical_pc() + ""));
        mDatas.add(new SalaryTaxBean(2,"公积金",mRuleData.getC_provident_fund_pc() + ""));

        mAdapter = new SalaryWXYJAdapter(mActivity);
        mAdapter.bindData(mDatas);
        mListView.setAdapter(mAdapter);

    }

    /**
     * 保存设置
     */
    private void saveData(){
        for (int i = 0; i < mDatas.size(); i++) {
            SalaryTaxBean bean = mDatas.get(i);
            if (bean.getType() != 0){
                LinearLayout layout = (LinearLayout) mListView.getChildAt(i);// 获得子item的layout
                RelativeLayout layout1 = (RelativeLayout) layout.findViewById(R.id.rl_wxyj_item_content);
                TextView tv = (TextView) layout1.findViewById(R.id.tv_wxyj_item_name);
                String name = tv.getText().toString();
                EditText et = (EditText) layout1.findViewById(R.id.ed_wxyj_item_number);// 从layout中获得控件,根据其id
                // EditText et = (EditText) layout.getChildAt(1) //或者根据位置
                String money = et.getText().toString().trim();
                if (StringUtil.isEmpty(money)){
                    money = "0";
                }
                if (bean.getType() == 1){ // 个人
                    mPerson.put(name,Float.parseFloat(money)/100);
                }else if (bean.getType() == 3){ // 医疗+
                    mPerson.put(name,Float.parseFloat(money));
                }else if (bean.getType() == 2){ // 公司
                    mCompany.put(name,Float.parseFloat(money)/100);
                }
            }
        }
        if (mPerson.size() == 0 || mCompany.size() == 0){
            showShortToast("数据错误");
        }else{
            setSalaryWXYJ();
        }
        Log.d("My_data","person" + mPerson.toString() + "\n company" + mCompany.toString());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                saveData();
                break;
        }
    }

    /**
     * 设置五险一金
     */
    private void setSalaryWXYJ(){
        waitDlgShow();
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_SID, LocalLogin
              .getInstance().mSid));
        nvps.add(new BasicNameValuePair(AsynHttpClient.KEY_GROUP_ID, mGroupId));
        nvps.add(new BasicNameValuePair("p_pension_pc", mPerson.get("养老") + ""));
        nvps.add(new BasicNameValuePair("p_medical_pc", mPerson.get("医疗") + ""));
        nvps.add(new BasicNameValuePair("p_medical_count", mPerson.get("医疗+") + ""));
        nvps.add(new BasicNameValuePair("p_provident_fund_pc", mPerson.get("公积金") + ""));
        nvps.add(new BasicNameValuePair("p_unemployment_pc", mPerson.get("失业") + ""));

        nvps.add(new BasicNameValuePair("c_pension_pc", mCompany.get("养老") + ""));
        nvps.add(new BasicNameValuePair("c_medical_pc", mCompany.get("医疗") + ""));
        nvps.add(new BasicNameValuePair("c_provident_fund_pc", mCompany.get("公积金") + ""));
        nvps.add(new BasicNameValuePair("c_unemployment_pc", mCompany.get("失业") + ""));
        nvps.add(new BasicNameValuePair("c_injury_pc", mCompany.get("工伤") + ""));
        nvps.add(new BasicNameValuePair("c_birth_pc", mCompany.get("生育") + ""));

        WithoutObjMsg msg = new WithoutObjMsg(mHandler.obtainMessage(SET_SALARY_WXYJ));
        msg.mApi = AsynHttpClient.API_SALARY_SET_WXYJ_PC;
        msg.mParams = nvps;
        AsynHttpClient.getInstance(this).execEventHttp(msg);
    }
}
